import {getDate, convertDate, getTime} from '../../utils/date';
import {
  selectByOneJoinTeamApi,
  deleteTeamApi,
  returnTeamApi,
  editPlanApi,
  editAgrencyApi,
} from '../../api/app';
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    show: false,
    list: [],
    count: [],
    currentDate: getDate (new Date (), '-'),
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData ({
      openid: wx.getStorageSync ('openId'),
    });
  },
  onShow: function (options) {
    this.getListDate ();
    this.setData ({
      openid: wx.getStorageSync ('openId'),
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来加入协同小程序吧~',
    };
  },
  toCreact () {
    wx.navigateTo ({
      url: '/subPages/clockin/create-book/index',
    });
  },
  toInfo (e) {
    const {id} = e.currentTarget.dataset;
    wx.navigateTo ({
      url: `/pages/agency-info/index?id=${id}`,
    });
  },
  // 获取当前团队
  async getListDate () {
    const openID = wx.getStorageSync ('openId');
    try {
      // read或空表示阅读打卡，sport表示运动打卡
      const res = await selectByOneJoinTeamApi ({
        openID,
      });
      if (res.code == 200 && res.result) {
        this.setData ({
          list: res.result.all || [],
          count: res.result.count || []
        });
      } else {
        this.setData ({
          team: null,
        });
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 创建团队
  toClockinPlan () {
    wx.navigateTo ({
      url: '/subPages/clockin/create-book/index',
    });
  },
  // 加入团队
  toClockin () {
    wx.navigateTo ({
      url: '/subPages/joinTeam/index',
    });
  },
  onChange (e) {
    const {id} = e.currentTarget.dataset;
    const status = e.detail ? '1' : '0';
    this.editState (status, id);
  },
  // 修改状态
  async editState (status, id) {
    try {
      const res = await editAgrencyApi ({
        status,
        id,
      });
      if (res.code == 200) {
        this.getListDate()
      }
      // wx.showToast ({
      //   title: res.msg,
      //   icon: 'none',
      // });
    } catch (error) {
      console.log (error);
    }
  },
  // 监听输入
  bindinput (e) {
    console.log (e);
    const {index} = e.currentTarget.dataset;
    const {value} = e.detail;
    this.setData ({
      currenteInput: {
        id: index,
        planPerPlan: value,
      },
    });
  },
  bindfocus (e) {
    const {index} = e.currentTarget.dataset;
    const {value} = e.detail;
    this.setData ({
      currenteInput: {
        id: index,
        planPerPlan: value,
      },
    });
  },
  handleReturn () {
    wx.showModal ({
      title: '提示',
      content: '你确定要退出当前团队？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.backTeam (wx.getStorageSync ('openId'));
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  handleDelete () {
    wx.showModal ({
      title: '提示',
      content: '你确定要解散当前团队？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.deleteTeam ();
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  handleRemove (e) {
    wx.showModal ({
      title: '提示',
      content: '你确定要移除该成员？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        const {index} = e.currentTarget.dataset;
        if (result.confirm) {
          this.backTeam (index);
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  // 退出团队
  async backTeam (id) {
    try {
      const res = await returnTeamApi ({
        openID: id,
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '退出成功！',
          icon: 'none',
        });
        setTimeout (() => {
          this.getPlanInfo ();
        }, 1500);
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 删除团队
  async deleteTeam () {
    try {
      const res = await deleteTeamApi ({
        openID: wx.getStorageSync ('openId'),
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '解散成功！',
          icon: 'none',
        });
        setTimeout (() => {
          this.getPlanInfo ();
        }, 1500);
      }
    } catch (error) {
      console.log (error);
    }
  },
});
